//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Last Modif: 13/03/2005
//

var MOD_CLTR={
  unkcls:[],
  shown:0,
  treeminrows:0
}



MOD_CLTR.printhook=function() {
  var html;  
  MOD_CLTR.treeminrows=1;
  html=MOD_CLTR.treecode(MOD_CLTR.treeminrows)+cltree("buttons");
  MOD_CLTR.treeminrows=0;
  return html;
}

MOD_CLTR.treecode=function(minrows) {
  var tcode='<scrip'+'t>var _tfs=new Array();\n';
  tcode+='var minrows='+(1+minrows)+';\n';
  tcode+='function addrow(t,cl,span) {  if(!_tfs[cl]){_tfs[cl]=minrows;};';
  tcode+='var r=t.insertRow(_tfs[cl]);';
  tcode+='lcell=r.insertCell(0);';
  tcode+='lcell.colSpan=span;';
  tcode+='lcell.style.border=0;';
  tcode+='var textn=document.createTextNode((_tfs[cl])+":"+lcell.width);';
  tcode+='lcell.appendChild(textn);';
  tcode+='_tfs[cl]++;}\n';
  
  tcode+='function delrow(t,cl) {';
  tcode+='if(_tfs[cl]>1) {';
  tcode+='t.deleteRow(--_tfs[cl]);}}</scrip'+'t>';
  
  return tcode;
}


MOD_CLTR.treeclickonclaim=function(cl) {
  SetFilter("List",cl);
  InitPageClaimIterator(); // not very clean
  edit_features();
}    

MOD_CLTR._claimtree=function(cl,options) {
  var ndep=0;
  var indep=0;
  var deplist=new Array();
  var html="";
  var clinfo="";
  var k=1;
  var Bstyle;
  
  var dep=GetClaimDep(cl);  
  if(dep) {   
    if(options=="buttons") {
      Bstyle="style=width=92% ";
    } else {
      Bstyle="style=width=100% ";
    }
    var cl_iter= new claim_iterator(cl);
    while(!cl_iter.end()) { //(var i=cl; i<= GetClaimSetLength(); i++) {     
      //document.write("check ("+deplist[i] + ", "+cl +")<BR>");
	if(cl_iter.getDep()==cl) {
	  //document.write(i + "depends on "+cl +"<BR>"); 
	  deplist[ndep++]=cl_iter.getValue();
	}
	cl_iter.next();
    }
    
    
    var tclass="";
    var mclass="";
    
    if(ndep || dep_isindependent(dep)) {   	
      k=(ndep?ndep:1);
      indep=1;    
    } else {
      k=1;
    }

    if(IsClaimInFilter(cl)) { 
      MOD_CLTR.shown++;
      mclass=" class='"+ClaimTypeStyle(cl)+"'";
      if(claim_isgrpindependent(cl)) {
	tclass='CLTREE CLINDEP'+GetClaimCategory(cl);
      } else {
	tclass='CLTREE CLDEP'+GetClaimCategory(cl);
      }
    } else {        
      tclass='HIDEN';
    }
    
    var htbut="";
    if(options=="buttons") {
      html+="<TABLE width=100% id=tab"+cl+" border=1 class='CLTREE'>";           
      html+="<TR><TH width=100% colspan="+k+" align=center CLASS='"+tclass+"'>"; 
      
      html+="<input type=button style='width=4%'  value='-' onclick='delrow(tab"+cl+","+cl+");'>";
      html+="<input type=button "+Bstyle+mclass+" value='" + cl;
    } else {
      html+="<TABLE width=100% id=tab"+cl+" border=0 class='CLTREE'>";           
      html+="<TR><TH width=100% colspan="+k+" align=center CLASS='"+tclass+"'>";       
      html+="<button style='width=100%' CLASS='"+tclass+"' onclick='MOD_CLTR.treeclickonclaim("+cl+");'>";
      html+=html_whatis(cl,GetClaimDescription(cl));
    }
    
    if(dep_isindependent(dep)) {	
      html+=" ("+ ClaimTypeCapName(cl) + ") ";	
    }	
    
    if(options =="buttons") {
      if(GetGroupsLength() >1) {
	html+=" ["+GetClaimGroup(cl)+"]";
      } 
      if(GetClaimEq(cl)) {
	html+=" ="+GetClaimEq(cl);
	if(GetGroupsLength() >1) {
	  html+=" ["+GetClaimGroup(GetClaimEq(cl))+"]";
	}   
      }
      html+="'>";
      html+="<input type=button style='width=4%' value='+'  onclick='addrow(tab"+cl+","+cl+","+k+");'>";
      
    } else {
      if(GetGroupsLength() >1) {
	html+=" ["+GetClaimGroup(cl)+"]";
      } 
      if(GetClaimEq(cl)) {
	html+=" ="+GetClaimEq(cl);
	if(GetGroupsLength() >1) {
	  html+=" ["+GetClaimGroup(GetClaimEq(cl))+"]";
	}   
      }
      
      if(IsClaimNotNovel(cl)) {
	html+=html_img(notnew_img);
      } else if(IsClaimNotInventive(cl)) {
	html+=html_img(notinv_img);
      } else if(IsClaimNovelandInventive(cl)) {
	html+=html_img(newinv_img);
      } else {
	html+=html_img(notsearched_img);
      }
      html+="</button>";
    }   
    html+="</TH></TR>";

    if(MOD_CLTR.treeminrows>0 && !GetClaimEq(cl)) {
      var mr=1;
      while(mr <= MOD_CLTR.treeminrows) {
	html+="<TR><TD align=left style='border-bottom-width:0px' colspan='"+k+"'>"+mr+":</TD></TR>";
	mr++;
      }
    }

    if(indep) {
      html+="<TR>";
      for(var i=0; i< ndep; i++) {
        html+="<TD align=center valign=top>";
        html+=MOD_CLTR._claimtree(deplist[i],options);
        html+="</TD>";      
      }
      html+="</TR>";
    }   
    
    html+="</TABLE>";  
    return html;	
  }
}

MOD_CLTR._clunktree=function() {
  if(MOD_CLTR.unkcls.length >0) {
    var html="<table><TR><TH>Unkown Dep </th></TR>";
    html+="<TR><TABLE><TR>";
    for(var cl in MOD_CLTR.unkcls) {
      html+="<TD>"+cl+"</TD>";
    }
    html+="</TR></TABLE></TR></TABLE>";
    return html;
  } 
  return "";
}


MOD_CLTR.checkIntegrity=function() {
  for(var cl_iter=new claim_iterator();!cl_iter.end();cl_iter.next()) {
    if(!cl_iter.getDep() || !cl_iter.getDep()=="E") {
      return cl_iter.getValue();
    }
  }
  return 0;
}

//
// public:
//  


// main function 
 
function cltree(options) {
  MOD_CLTR.unkcls=new Array();
  CURRENTVIEW="claimtree";
  ACT_MOD=MOD_CLTR;
  var errcl=MOD_CLTR.checkIntegrity();
  if(errcl) {
    return "<H3>Please, fix dependency problems for claim "+errcl+" first</h3>";
  }
  var html="<TABLE  valign=top width=100% >";
  var cl_iter= new claim_iterator();
  while(!cl_iter.end()) {
    var dep=cl_iter.getDep();
    if(dep) {
      if(dep_isindependent(dep)){
        MOD_CLTR.shown=0;
        var subtree=MOD_CLTR._claimtree(cl_iter.getValue(),options);
        if(MOD_CLTR.shown>0) {  
          html+="<TR><TD width=100%>";		   
          html+=subtree;
          html+="<BR><BR></TD></TR>";
        }
      }
    } else {
      MOD_CLTR.unkcls.push(cl_iter.getValue());
    }		
    cl_iter.next();
  }
  html+="<TR><TD>"+MOD_CLTR._clunktree()+"</TD></TR>";
  html+="</TABLE></P>";
  return html;
}

